﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;

namespace FinalTest_DAO
{
    class Connection
    {
        private static Connection instance = null; //Declare a instance of the class Connection

        private SqlConnection connection; //Declare a SQL Connection

        private string connectionString; //Declare a connection string

        /// <summary>
        /// Constructor of the class Connection
        /// </summary>
        private Connection()
        {
            connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["FinalTestConnectionString"].ToString();
            connection = new SqlConnection(connectionString);
        }

        /// <summary>
        /// The method get instance of the class Connecttion
        /// </summary>
        /// <returns>
        /// return a instace of the class Connection
        /// </returns>
        public static Connection GetInstace()
        {
            if (instance == null)
            {
                instance = new Connection();
            }

            return instance;
        }

        /// <summary>
        /// Open connection method
        /// </summary>
        public void Open()
        {
            try
            {
                if (connection.State == ConnectionState.Closed)
                {
                    connection.Open();
                }
            }
            catch (SqlException ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Close connection method
        /// </summary>
        public void Close()
        {
            if (connection.State == ConnectionState.Open)
            {
                connection.Close();
            }
        }
    }
}
